﻿using DataAccess.DTO;
using DataAccess.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Controllers
{
    public class StatisticController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }
       
        /// <summary>
        /// 个人推广详情
        /// </summary>
        /// <returns></returns>
        public ActionResult InComeList()
        {
            return View();
        }
         [HttpPost]
        /// <summary>
        /// 个人推广详情数据接口
        /// </summary>
        /// <returns></returns>
        public ActionResult GetIncome()
        {
            DataAccess.DAL.ProductEffectDAL dal = new DataAccess.DAL.ProductEffectDAL();



            int limit = XYD.Common.Request.GetFormInt("limit", 20);
            int offset = XYD.Common.Request.GetFormInt("offset", 1);

            if (limit > 100) { limit = 10; }

            //Starttime: $("#Starttime").val(), Endtime: $("#Endtime").val(), tgw: $("#tgw").val(), aliID: $("#aliID").val(), userid: $("#userid").val()
            string Starttime = XYD.Common.Request.GetFormString("Starttime").Trim();
            string Endtime = XYD.Common.Request.GetFormString("Endtime").Trim();
            string tgw = XYD.Common.Request.GetFormString("tgw").Trim();
            string userid = XYD.Common.Request.GetFormString("userid").Trim();
            string aliID = XYD.Common.Request.GetFormString("aliID").Trim();
            string strWhere = " 1=1 ";
            if (Starttime != "")
            {
                strWhere = strWhere + " AND CreateDate>='" + Starttime + "'";
            }
            if (Endtime != "")
            {
                strWhere = strWhere + " AND CreateDate<='" + Endtime + "'";
            }
            if (tgw != "0")
            {
                strWhere = strWhere + " AND PromotionType=" + tgw;
            }
            if (userid != "")
            {
                strWhere = strWhere + " AND PromotionBit='" + userid + "'";
            }
            if (aliID != "")
            {
                strWhere = strWhere + " AND AlimamaID='" + aliID + "'";
            }
            int count = 0;

            List<ProductEffect> list = dal.ListPage(UserSession.user_id,strWhere, "", limit, (offset / limit) + 1, out count);

            Result result = new Result();
            result.State = true;
            result.Message = "查询成功";
            result.Datas = new PageData<ProductEffect> { Total = count, Rows = list, PageIndex = (offset / limit) + 1, PageSize = limit };

            return Json(result);
        }
        /// <summary>
        /// 个人推广统计
        /// </summary>
        /// <returns></returns>
        public ActionResult IncomeStatList()
        {
            return View();
        }
        [HttpPost]
        /// <summary>
        /// 个人推广报表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetUserExtension()
        {
            DataAccess.DAL.ProductEffectDAL dal = new DataAccess.DAL.ProductEffectDAL();
            string Starttime = XYD.Common.Request.GetFormString("Starttime").Trim();
            string Endtime = XYD.Common.Request.GetFormString("Endtime").Trim();
            string tgw = XYD.Common.Request.GetFormString("tgw").Trim();
            string userid = XYD.Common.Request.GetFormString("userid").Trim();
            string aliID = XYD.Common.Request.GetFormString("aliID").Trim();
            string strWhere = " where 1=1 ";
            if (Starttime != "")
            {
                strWhere = strWhere + " AND CreateDate>='" + Starttime + "'";
            }
            if (Endtime != "")
            {
                strWhere = strWhere + " AND CreateDate<='" + Endtime + "'";
            }
            if (tgw != "0"&&tgw!="")
            {
                strWhere = strWhere + " AND PromotionType=" + tgw;
            }
            if (userid != "")
            {
                strWhere = strWhere + " AND PromotionBit='" + userid + "'";
            }
            if (aliID != "")
            {
                strWhere = strWhere + " AND AlimamaID='" + aliID + "'";
            }
            List<UserCratDTO> list = dal.GetUserCrat(strWhere,UserSession.user_id);
            Result result = new Result();
            result.State = true;
            result.Message = "查询成功";
            result.Datas = new PageData<UserCratDTO> { Total = 1, Rows = list};
            return Json(result);
        }
        /// <summary>
        /// 推广结算分析
        /// </summary>
        /// <returns></returns>
        public ActionResult PayAnalysis()
        {
            return View();
        }
         [HttpPost]
        /// <summary>
        /// 推广收入分析
        /// </summary>
        /// <returns></returns>
         public ActionResult GetPayIncome()
        {
            DataAccess.DAL.ProductEffectDAL dal = new DataAccess.DAL.ProductEffectDAL();
            string Starttime = XYD.Common.Request.GetFormString("Starttime").Trim();
            string Endtime = XYD.Common.Request.GetFormString("Endtime").Trim();
            int Income = XYD.Common.Request.GetFormInt("Income",1);

            string sortName=XYD.Common.Request.GetFormString("sortName").Trim();
            string sortOrder=XYD.Common.Request.GetFormString("sortOrder").Trim();



            string strWhere = "";
            if (Starttime != "")
            {
                strWhere = strWhere + " AND CreateDate>='" + Starttime + "'";
            }
            if (Endtime != "")
            {
                strWhere = strWhere + " AND CreateDate<='" + Endtime + "'";
            }
            string order = "";
             if(sortName!=""&&sortOrder!=""){
                 order = " order by " + sortName + " " + sortOrder;
             }
             List<ProductEffectDTO> list = dal.GetPayAnalysis(strWhere, Income, order,UserSession.user_id);

             
             var rate=((100-SysConfig.rate_all)/100.00);

            list.ForEach(l => {
                l.Income = Math.Round(l.Income * rate,2);
                l.ChildIncome =Math.Round( l.ChildIncome * rate,2);
                l.LowestIncome =Math.Round( l.LowestIncome * rate,2);
                l.SumIncome = l.Income + l.ChildIncome + l.LowestIncome;
            });

             //0.85


            Result result = new Result();
            result.State = true;
            result.Message = "查询成功";
            result.Datas = new PageData<ProductEffectDTO> { Total = 0, Rows = list};
            return Json(result);
        }
        

        /// <summary>
        /// 近期业绩推广
        /// </summary>
        /// <returns></returns>
        public ActionResult RecentPerformancePromotion()
        {
            return View();
        }

        /// <summary>
        /// 淘宝订单号查询
        /// </summary>
        /// <returns></returns>
        public ActionResult TaobaoOrderNumber()
        {
            return View();
        }

        /// <summary>
        /// 淘宝成交订单查看
        /// </summary>
        /// <returns></returns>
        public ActionResult TaobaoOrderView()
        {
            return View();
        }

        /// <summary>
        /// 推广链接转换
        /// </summary>
        /// <returns></returns>
        public ActionResult ExtendedLinkTransformation()
        {
            return View();
        }

        /// <summary>
        /// 个人推广详情
        /// </summary>
        /// <returns></returns>
        public ActionResult PersonalPromotionDetails()
        {
            return View();
        }

        /// <summary>
        /// 个人推广统计
        /// </summary>
        /// <returns></returns>
        public ActionResult PersonalPromotionStatistics()
        {
            return View();
        }

        /// <summary>
        /// 代理推广分析
        /// </summary>
        /// <returns></returns>
        public ActionResult AgentExtensionAnalysis()
        {
            return View();
        }

        /// <summary>
        /// 代理推广统计
        /// </summary>
        /// <returns></returns>
        public ActionResult AgencyPromotionStatistics()
        {
            return View();
        }

        /// <summary>
        /// 推广收入分析
        /// </summary>
        /// <returns></returns>
        public ActionResult PromotionIncomeAnalysis()
        {
            return View();
        }

        //近期业绩推广 Recent performance promotion
        //淘宝订单号查询 Taobao order number
        //淘宝成交订单查看 Taobao order view
        //推广链接转换 Extended link transformation
        //个人推广详情 Personal promotion details
        //个人推广统计 Personal promotion statistics
        //代理推广分析 Agent extension analysis
        //代理推广统计 Agency promotion statistics
        //推广收入分析 Promotion income analysis

    }
}